package com.example.patients;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.patients.adapter.Adapter_update;
import com.example.patients.database.DbPatients;
import com.example.patients.entity.PatientsBean;

import java.util.ArrayList;
import java.util.List;

public class ModPatientsActivity extends AppCompatActivity implements View.OnClickListener {
    ListView UpdateLv;//列表
    EditText EtNumber;//病床号
    List<PatientsBean> mData;//数据源
    Adapter_update adapter;//适配器对象
    DbPatients dbPatients;//数据库

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mod_patients);//绑定事件
        UpdateLv = findViewById(R.id.update_lv);
        EtNumber = findViewById(R.id.et_update);
        mData = new ArrayList<>();
        adapter = new Adapter_update(this, mData);
        UpdateLv.setAdapter(adapter);//适配器列表显示
        dbPatients = new DbPatients(this);

    }
    /**
     * 按钮点击事件
     * */
    @Override
    public void onClick(View v) {
        int id=v.getId();
        if(id==R.id.iv_back){
            Intent intent = new Intent(ModPatientsActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        } else if (id== R.id.update_iv) {
            String number = EtNumber.getText().toString().trim();
            PatientsBean patientsBeans = dbPatients.QueryFromPatientByNumber(number);//查询病床号号是否存在
            if (TextUtils.isEmpty(number)) {
                Toast.makeText(this, "请输入病床号！", Toast.LENGTH_SHORT).show();
            } else if (patientsBeans == null) {
                Toast.makeText(this, "该病床号不存在！", Toast.LENGTH_SHORT).show();
            } else {
                //开始搜索
                List<PatientsBean> list = DbPatients.getPatientListByNumber(number);//查询该病床号的所有信息
                mData.clear();//将原来搜索的消息清除
                mData.addAll(list);
                adapter.notifyDataSetChanged();//实时更新
                initData();//修改信息函数
            }
        }
    }

    /**
     * 修改信息点击事件，点击就会进入到修改页面进行修改
     * */
    private void initData() {
        dbPatients = new DbPatients(this); //创建数据库
        UpdateLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                PatientsBean patientsBean = mData.get(position);
                Intent intent = new Intent(ModPatientsActivity.this, AddPatientsActivity.class);
                intent.putExtra("number", patientsBean.getNumber());
                intent.putExtra("name", patientsBean.getName());
                intent.putExtra("year", patientsBean.getYear());
                intent.putExtra("sex", patientsBean.getSex());
                intent.putExtra("sickness", patientsBean.getSickness());
                intent.putExtra("taboo", patientsBean.getTaboo());
                intent.putExtra("phone", patientsBean.getPhone());
                ModPatientsActivity.this.startActivityForResult(intent, 1);
            }
        });
    }
}
